package cache_service

import (
	"cachecron/dao"
	"cachecron/global"
	"encoding/json"
	"fmt"
)

type CacheData struct {
	SearchName    string
	SearchContent any
	UserID        string
}

func StartService() {
	fmt.Println("服务端启动")
	var cache_data CacheData
	for {
		result, err := global.App.Redis.RPop("Cache").Result()

		if err != nil {

		} else {

			global.App.Redis.SRem("CacheSet", result)
			json.Unmarshal([]byte(result), &cache_data)
			marshal, _ := json.Marshal(cache_data.SearchContent)

			data := map[string]string{
				"search_name":    cache_data.SearchName,
				"search_content": string(marshal),
				"user_id":        cache_data.UserID,
			}

			dao.InsertCacheData(data)
		}

	}

	select {}
}
